import { setCurrentUser } from '@/utils/user';
import { getImageById } from '@/utils/utils';
import { get } from 'lodash';
import type { Model } from './connect';
import UserPng from '@/assets/images/user.png';
import userAggregateInfo from '@/constant/userAggregateInfo-mock';
import { loginCallback } from '@/utils/login';

const initialState = {
  currentUser: {} as UserState,
  isSuperAdmin: false,
  permission: [],
  delegations: [],
  delegateMode: false,
  role: [],
};

export interface CurrentUser {
  currentUser: {
    avatar: string;
    isSuperAdmin: boolean;
    id: string;
    userLoginName: string;
    notifyCount: number;
    unreadCount: number;
  };
}
export interface UserState {
  currentUser: any;
  isSuperAdmin: boolean;
  permission: string[];
}

interface User extends Model {
  state: UserState;
}
interface DelegationItem {
  delegateCertificateId: string;
  ownerUserCode: string;
  ownerUserName: string;
}

interface Role {
  id: string;
  roleCode: string;
  roleName: string;
}

interface Menu {
  id: string;
  parentResourceId: string;
  type: string;
  path: string;
  name: string;
  seqNum: number;
  leaf: boolean;
  children: any[];
  optCounter: number;
  key: string;
  openTabPath: string;
}

interface UserInfo {
  avatar: string;
  email: string;
  id: string;
  isActive: boolean;
  organizationId: string;
  staffNumber: string;
  userLoginName: string;
  userName: string;
  userType: string;
}

interface UserAggregateInfoResponse {
  delegations: DelegationItem[];
  functions: string[];
  isSuperAdmin: boolean;
  roles: Role[];
  startMenus: Menu[];
  user: UserInfo;
}

const UserModel: User = {
  namespace: 'user',
  state: initialState,
  effects: {
    *fetchCurrent({ payload = {} }, { put }) {
      const { route } = payload;
      const res: UserAggregateInfoResponse = userAggregateInfo;
      const { delegations, functions, isSuperAdmin, roles, startMenus, user } = res;

      const userInfo = { empNo: user.id, nickName: user.userName };
      // 注册神策数据采集器
      loginCallback(userInfo);

      //处理用户头像
      user.avatar = UserPng;
      const pictureId = get(user, 'pictureId');
      if (pictureId) {
        user.avatar = getImageById(pictureId);
      }
      setCurrentUser(user);

      // 初始化菜单
      yield put({
        type: 'menu/initMenu',
        payload: {
          route,
          startMenus,
        },
      });
      // 保存当前用户的角色，菜单等信息
      yield put({
        type: 'save',
        payload: {
          currentUser: user,
          isSuperAdmin,
          permission: functions,
          delegations,
          delegateMode: false,
          role: roles,
        },
      });
    },
  },
  reducers: {
    save(state, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
    // 通知账户
    changeNotifyCount(state, { payload }: any) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: payload.totalCount,
          unreadCount: payload.unreadCount,
        },
      };
    },
  },
};
export default UserModel;
